package com.cnswhy.cloud.server.system.controller;

import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.redis.service.RedisService;
import com.cnswhy.cloud.server.system.dto.WithdrawalRecordPageDTO;
import com.cnswhy.cloud.server.system.dto.WithdrawalRecordSaveDTO;
import com.cnswhy.cloud.server.system.dto.WithdrawalRecordUpdateDTO;
import com.cnswhy.cloud.server.system.dto.WithdrawalRecordVerifyDTO;
import com.cnswhy.cloud.server.system.entity.WithdrawalRecord;
import com.cnswhy.cloud.server.system.service.WithdrawalRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
//import com.github.zuihou.security.annotation.PreAuth;


/**
 * <p>
 * 前端控制器
 * 
 * </p>
 *
 * @author cnswhy
 * @date 2020-10-10
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/withdrawalRecord")
@Api(value = "WithdrawalRecord", tags = "")
//@PreAuth(replace = "withdrawalRecord:")
public class WithdrawalRecordController extends SuperController<WithdrawalRecordService, Long, WithdrawalRecord, WithdrawalRecordPageDTO, WithdrawalRecordSaveDTO, WithdrawalRecordUpdateDTO> {
    @Autowired
    RedisService redisService;

    public static final String WITHDRAW_VERIFY_SUCCESS = "withdraw_verify_success";

    public static final String WITHDRAW_VERIFY_REFUSE = "withdraw_verify_refuse";
    @ApiOperation(value = "提现审核")
    @PostMapping("/verifyStatus")
    public R<Object> verifyStatus(@RequestBody WithdrawalRecordVerifyDTO withdrawalRecordVerifyDTO) {
        WithdrawalRecord withdrawalRecord = baseService.getById(withdrawalRecordVerifyDTO.getId());
        if(withdrawalRecord.getVerifyStatus() > 1){
            return fail("请勿重复审核");
        }
        withdrawalRecord.setVerifyStatus(withdrawalRecordVerifyDTO.getVerifyStatus());
        withdrawalRecord.setRemark(withdrawalRecordVerifyDTO.getRemark());
        baseService.updateById(withdrawalRecord);
        if(withdrawalRecordVerifyDTO.getVerifyStatus() == 2){
            redisService.hset(WITHDRAW_VERIFY_SUCCESS, withdrawalRecord.getId().toString(), withdrawalRecord);

        }else{
            redisService.hset(WITHDRAW_VERIFY_REFUSE, withdrawalRecord.getId().toString(), withdrawalRecord);

        }
        //异步处理
        return success(withdrawalRecord);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<WithdrawalRecord> withdrawalRecordList = list.stream().map((map) -> {
            WithdrawalRecord withdrawalRecord = WithdrawalRecord.builder().build();
            //TODO 请在这里完成转换
            return withdrawalRecord;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(withdrawalRecordList));
    }
}
